package com.ocm.commerceinteraction.ui;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.ocm.commerceinteraction.GlobalApplication;
import com.ocm.commerceinteraction.MainActivity;
import com.ocm.commerceinteraction.R;
import com.ocm.commerceinteraction.config.RequestAddr;
import com.ocm.commerceinteraction.model.BaseModel;
import com.ocm.commerceinteraction.model.SearchUserList;
import com.ocm.commerceinteraction.ui.adapter.SearchUserListAdapter;
import com.ocm.commerceinteraction.util.http.AjaxCallBack;
import com.ocm.commerceinteraction.util.loading.Loading;
import com.ocm.commerceinteraction.util.view.SearchEditText;
import com.ocm.commerceinteraction.util.view.percentsupport.PercentRelativeLayout;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

@EFragment(R.layout.fragment_search_user)
public class SearchUserFragment extends BaseFragment {


    @ViewById(R.id.toptitle_title)
    TextView topTitle;
    @ViewById(R.id.back_btn)
    PercentRelativeLayout backBtn;

    @ViewById(R.id.search)
    SearchEditText searchEditText;

    @ViewById(R.id.list)
    ListView listView;

    SearchUserListAdapter searchUserLisrAdapter;

    @Override
    @AfterViews
    void initView() {
        topTitle.setText("添加好友");
        backBtn.setVisibility(View.VISIBLE);


        searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    /*隐藏软键盘*/
                    InputMethodManager imm = (InputMethodManager) v
                            .getContext().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(
                                v.getApplicationWindowToken(), 0);
                    }
                    searchUser(v.getText().toString().trim());
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    @Click(R.id.back_btn)
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getFragmentMangerinstance().popBackStack();
                break;
        }
    }

    void searchUser(String keywords) {
        requestUrl = RequestAddr.searchUser +
                "?keywords=" + keywords +
                "&p=1";

        GlobalApplication.xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                Loading.stopLoading();
                dealData(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                Loading.stopLoading();
            }
        });
    }

    SearchUserList searchUserList;

    void dealData(Object object) {
        try {
            searchUserList = gson.fromJson(object.toString(), SearchUserList.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }
        searchUserLisrAdapter = new SearchUserListAdapter(getActivity(), searchUserList);
        listView.setAdapter(searchUserLisrAdapter);

    }

//    @TextChange(R.id.search)
//    void onTextChangesOnHelloTextView(CharSequence text, TextView hello, int before, int start, int count) {
//        LogHelper.i("text:"+text);
//        LogHelper.i("hello:"+hello);
//        LogHelper.i("before:"+before);
//        LogHelper.i("start:"+start);
//        LogHelper.i("count:"+count);
//    }
}
